/**
 * Created by mahai on 2019/1/16.
 * 简历接口
 */

const express = require('express');
const router = express.Router();
const passport = require('passport');
const Resume = require('../../models/Resume');

router.get('/',(req,res)=>{
    res.send (`
        status:0 失败 1 不存在 2 成功
        /**
        * [POST] /add
        *  {
        *      userId:用户id  必填
        *      job:{                   必填
        *          position:"" 职位
        *          workingPlace:'',    工作地点
        *          salary:0,           薪资
        *          status:0            0: 系统生成 1: 全职  2 ：兼职
        *      }
        *      sill:[]     技能特长 必填
        *      name:“” 姓名 必填
        *      sex：'' 性别 必填
        *      age：0 岁数 必填
        *      race：" " 种族 必填
        *      education： "" 学历 必填
        *      major:'' 专业
        *      school:[] 学校 必填
        *      project:[] 项目 必填
        *      interest:[] 兴趣 必填
        *      evaluation: '' 自我评价 必填
        *      avatar:'' 头像 
         *      qq:''   qq号
         *      phone：'' 手机号码
         *      email：'' email
        *  }
        * */
        
        /***
        * [POST] edit/:id
        * *  {
        *      job:{                   必填
        *          position:"" 职位
        *          workingPlace:'',    工作地点
        *          salary:0,           薪资
        *          status:0            0: 系统生成 1: 全职  2 ：兼职
        *      }
        *      sill:[]     技能特长 必填
        *      name:“” 姓名 必填
        *      sex：'' 性别 必填
        *      age：0 岁数 必填
        *      race：" " 种族 必填
        *      education： "" 学历 必填
        *      major:'' 专业
        *      school:[] 学校 必填
        *      project:[] 项目 必填
        *      interest:[] 兴趣 必填
        *      evaluation: '' 自我评价 必填
        *      avatar:'' 头像
         *      qq:''   qq号
         *      phone：'' 手机号码
         *      email：'' email
        *  }
        */
        
         /***
         * [GET] find/:id
         */
         /***
         * [get] user/:id
         * 获取用户有没有的数据
         * */
    `);
});

/**
 * [POST] /add
 *  {
 *      userId:用户id  必填
 *      job:{                   必填
 *          position:"" 职位
 *          workingPlace:'',    工作地点
 *          salary:0,           薪资
 *          status:0            0: 系统生成 1: 全职  2 ：兼职
 *      }
 *      sill:[]     技能特长 必填
 *      name:“” 姓名 必填
 *      sex：'' 性别 必填
 *      age：0 岁数 必填
 *      race：" " 种族 必填
 *      education： "" 学历 必填
 *      major:'' 专业
 *      school:[] 学校 必填
 *      project:[] 项目 必填
 *      interest:[] 兴趣 必填
 *      evaluation: '' 自我评价 必填
 *      avatar:'' 头像
 *      qq:''   qq号
 *      phone：'' 手机号码
 *      email：'' email
 *  }
 *
 * */
router.post('/add',passport.authenticate('jwt',{session:false}),(req,res)=>{
    const resumeFile = {};
    if(req.body.userId) resumeFile.userId = req.body.userId;
    if(req.body.job) resumeFile.job = req.body.job;
    if(req.body.sill) resumeFile.sill = req.body.sill;
    if(req.body.name) resumeFile.name = req.body.name;
    if(req.body.sex) resumeFile.sex = req.body.sex;
    if(req.body.age) resumeFile.age = req.body.age;
    if(req.body.race) resumeFile.race = req.body.race;
    if(req.body.education) resumeFile.education = req.body.education;
    if(req.body.major) resumeFile.major = req.body.major;
    if(req.body.school) resumeFile.school = req.body.school;
    if(req.body.project) resumeFile.project = req.body.project;
    if(req.body.interest) resumeFile.interest = req.body.interest;
    if(req.body.evaluation) resumeFile.evaluation = req.body.evaluation;
    if(req.body.qq) resumeFile.qq = req.body.qq;
    if(req.body.phone) resumeFile.phone = req.body.phone;
    if(req.body.email) resumeFile.email = req.body.email;
    if(req.body.extra_params){
        let extra_params = req.body.extra_params;
        if(JSON.stringify(extra_params) === '{}'){
            resumeFile.extra_param = {"exports": "extra_params是扩展字段"};
        } else {
            resumeFile.extra_params = req.body.extra_params
        }
    }
    if(req.body.avatar) resumeFile.avatar = req.body.avatar;
    else resumeFile.avatar = "";
    Resume.findOne({userId:req.body.userId}).then(isResume=>{
        if(isResume){
            res.json({
                msg:'已存在！',
                status:1
            })
        } else {
            new Resume(resumeFile).save().then(resume => {
                res.json({
                    msg:'新增成功！',
                    status:2,
                    data:resume
                })
            }).catch(err=>res.status(400).json({
                status:0,
                msg:err
            }))
        }
    })
});

/***
 * [POST] edit/:id
 * *  {
 *      job:{                   必填
 *          position:"" 职位
 *          workingPlace:'',    工作地点
 *          salary:0,           薪资
 *          status:0            0: 系统生成 1: 全职  2 ：兼职
 *      }
 *      sill:[]     技能特长 必填
 *      name:“” 姓名 必填
 *      sex：'' 性别 必填
 *      age：0 岁数 必填
 *      race：" " 种族 必填
 *      education： "" 学历 必填
 *      major:'' 专业
 *      school:[] 学校 必填
 *      project:[] 项目 必填
 *      interest:[] 兴趣 必填
 *      evaluation: '' 自我评价 必填
 *      avatar:'' 头像
 *      qq:''   qq号
 *      phone：'' 手机号码
 *      email：'' email
 *  }
 */
router.post('/edit/:id',passport.authenticate('jwt',{session:false}),(req,res)=>{
    const resumeFile = {
        update_time:Date.now()
    };
    if(req.body.job) resumeFile.job = req.body.job;
    if(req.body.sill) resumeFile.sill = req.body.sill;
    if(req.body.name) resumeFile.name = req.body.name;
    if(req.body.sex) resumeFile.sex = req.body.sex;
    if(req.body.age) resumeFile.age = req.body.age;
    if(req.body.race) resumeFile.race = req.body.race;
    if(req.body.education) resumeFile.education = req.body.education;
    if(req.body.major) resumeFile.major = req.body.major;
    if(req.body.school) resumeFile.school = req.body.school;
    if(req.body.project) resumeFile.project = req.body.project;
    if(req.body.interest) resumeFile.interest = req.body.interest;
    if(req.body.evaluation) resumeFile.evaluation = req.body.evaluation;
    if(req.body.qq) resumeFile.qq = req.body.qq;
    else resumeFile.qq = '';
    if(req.body.phone) resumeFile.phone = req.body.phone;
    if(req.body.email) resumeFile.email = req.body.email;
    if(req.body.extra_params){
        let extra_params = req.body.extra_params;
        if(JSON.stringify(extra_params) === '{}'){
            resumeFile.extra_param = {"exports": "extra_params是扩展字段"};
        } else {
            resumeFile.extra_params = req.body.extra_params
        }
    }
    if(req.body.avatar) resumeFile.avatar = req.body.avatar;
    if(req.body.avatar == ''){
        resumeFile.avatar = '';
    }
    Resume.findOne({_id:req.params.id}).then(isResume=>{
        if(!isResume){
            res.json({
                msg:'已存在！',
                status:1
            })
        }
        Resume.findOneAndUpdate(
            {_id:req.params.id},
            {$set:resumeFile},
            {new:true}
        ).then(profile => res.json({
            data:profile,
            msg:'更新成功',
            status:2
        })).catch(err=>res.json({
            msg:'更新失败',
            status:1
        }))
    }).catch(err => res.status(404).json({
        data:err,
        status:0
    }))
});

/***
 * [GET] find/:id
 */
router.get('/find/:id',(req,res)=>{
    Resume.findOne({_id:req.params.id}).then(isResume=>{
        if(!isResume){
            res.json({
                msg:'不存在！',
                status:1
            })
        }
        res.json({
            data:isResume,
            msg:'获取成功',
            status:2
        })
    }).catch(err => res.json({
        msg:'不存在！',
        status:1
    }))
});
/***
 * [DELETE] delete/:id
 */
router.delete('/delete/:id',passport.authenticate('jwt',{session:false}),(req,res)=>{
    Resume.findOneAndRemove({_id:req.params.id}).then(profile=>{
        profile.save().then(profile=>res.json({
            data:profile,
            msg:'删除成功',
            status:2
        }))
    }).catch(err=>res.json({
        msg:'删除失败',
        status:1
    })).catch(err=>{
        res.status(400).json({
            msg:err,
            status:0
        })
    })
});
/***
 * [get] api/about/user/:id
 * 获取用户有没有关于的数据
 * */
router.get('/user/:id',(req,res)=>{
    Resume.findOne({userId:req.params.id}).then(resume=>{
        if(!resume){
            res.json({
                msg:'数据不存在',
                status:1
            });
        } else {
            res.json({
                msg:'存在',
                data:resume,
                status:2
            });
        }
    }).catch(err => res.status(404).json({
        data:err,
        status:0
    }))
});
module.exports = router;
